//
//  ClassifyListCell.m
//  B2C
//
//  Created by xiaowu on 2017/8/3.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "ClassifyListCell.h"

@interface ClassifyListCell ()

@property (nonatomic, strong) ClassifyItemView *firstView;
@property (nonatomic, strong) ClassifyItemView *secondView;
@property (nonatomic, strong) ClassifyItemView *thirdView;

@property (nonatomic, strong) ClassifyThirdModel *firstModel;
@property (nonatomic, strong) ClassifyThirdModel *secondModel;
@property (nonatomic, strong) ClassifyThirdModel *thirdModel;

@property (nonatomic, assign) CGFloat offset;
@end

@implementation ClassifyListCell

+ (NSArray *)getCellItemArr:(NSArray *)dataArr withIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *arr = [NSMutableArray array];
    
    ClassifyThirdModel *firstModel = [dataArr safeObjectAtIndex:(3 * indexPath.row)];
    [arr addObject:firstModel];
    if ((dataArr.count - 1) / 3 == indexPath.row) {
        // 判断是否是最后一行
        NSInteger index = (dataArr.count % 3);
        if (index == 2) {
            ClassifyThirdModel *secondModel = [dataArr safeObjectAtIndex:(3 * indexPath.row + 1)];
            [arr addObject:secondModel];
        } else if (index == 0) {
            ClassifyThirdModel *secondModel = [dataArr safeObjectAtIndex:(3 * indexPath.row + 1)];
            [arr addObject:secondModel];
            
            ClassifyThirdModel *thirdModel = [dataArr safeObjectAtIndex:(3 * indexPath.row + 2)];
            [arr addObject:thirdModel];
        }
    } else {
        ClassifyThirdModel *secondModel = [dataArr safeObjectAtIndex:(3 * indexPath.row + 1)];
        [arr addObject:secondModel];
        
        ClassifyThirdModel *thirdModel = [dataArr safeObjectAtIndex:(3 * indexPath.row + 2)];
        [arr addObject:thirdModel];
    }

    return arr;
}

+ (NSInteger)getRowNumberWithItemArr:(NSArray *)dataArr {
    
    return ((dataArr.count + 2) / 3);
}

+ (CGFloat)getHeightWithOffset:(CGFloat)offset {
    CGFloat height = 0;
    
    CGFloat width = ScreenWidth - offset;
    CGFloat itemWidth = width / 3.0;
    
    height += [ClassifyItemView getHeightWithWidth:itemWidth];
    
    return height;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        self.contentView.backgroundColor = [UIColor whiteColor];
        _firstModel = [[ClassifyThirdModel alloc] init];
        _secondModel = [[ClassifyThirdModel alloc] init];
        _thirdModel = [[ClassifyThirdModel alloc] init];
        _offset = 0.0;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    [self.contentView addSubview:self.firstView];
    [self.contentView addSubview:self.secondView];
    [self.contentView addSubview:self.thirdView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat width = ScreenWidth - self.offset;
    CGFloat itemWidth = width / 3.0;
    
    self.firstView.frame = CGRectMake(0, 0, itemWidth, [ClassifyItemView getHeightWithWidth:itemWidth]);
    self.secondView.frame = CGRectMake(itemWidth, 0, itemWidth, [ClassifyItemView getHeightWithWidth:itemWidth]);
    self.thirdView.frame = CGRectMake(itemWidth * 2, 0, itemWidth, [ClassifyItemView getHeightWithWidth:itemWidth]);
}

- (void)setValueForCell:(NSArray *)modelArr offset:(CGFloat)offset {
    self.offset = offset;
    
    NSInteger index = modelArr.count % 3;
    if (index == 1) {
        self.firstView.hidden = NO;
        self.secondView.hidden = YES;
        self.thirdView.hidden = YES;

    } else if (index == 2) {
        self.firstView.hidden = NO;
        self.secondView.hidden = NO;
        self.thirdView.hidden = YES;
    } else {
        self.firstView.hidden = NO;
        self.secondView.hidden = NO;
        self.thirdView.hidden = NO;
    }
    self.firstModel = [modelArr firstObject];
    [self.firstView setItemViewWithImageUrl:self.firstModel.gtrcimg title:self.firstModel.gtrcname];
    if (index != 1) {
        self.secondModel = [modelArr safeObjectAtIndex:1];
        [self.secondView setItemViewWithImageUrl:self.secondModel.gtrcimg title:self.secondModel.gtrcname];
        self.thirdModel = [modelArr lastObject];
        [self.thirdView setItemViewWithImageUrl:self.thirdModel.gtrcimg title:self.thirdModel.gtrcname];
    }

    
    

    __weak typeof(self) weakSelf = self;
    self.firstView.block = ^{
        if (weakSelf.block) {
            weakSelf.block(0);
        }
    };
    self.secondView.block = ^{
        if (weakSelf.block) {
            weakSelf.block(1);
        }
    };
    self.thirdView.block = ^{
        if (weakSelf.block) {
            weakSelf.block(2);
        }
    };
}

- (ClassifyItemView *)firstView {
    if (!_firstView) {
        _firstView = [[ClassifyItemView alloc] init];
    }
    return _firstView;
}

- (ClassifyItemView *)secondView {
    if (!_secondView) {
        _secondView = [[ClassifyItemView alloc] init];
    }
    return _secondView;
}

- (ClassifyItemView *)thirdView {
    if (!_thirdView) {
        _thirdView = [[ClassifyItemView alloc] init];
    }
    return _thirdView;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
